﻿using System.IO;
using System;
using System.Text;

namespace ConsoleApplication1
{
    //--------------------------------------------------------------------------
    public static class DS_CSV_server
    {
        static string fileName = "test.csv";
        static public string Clear()
        {
            File.Delete(fileName);
            File.AppendAllText(fileName, "");
            return "success";
        }

        static public string Create(string _person)
        {
            if (!File.Exists(fileName)) { File.AppendAllText(fileName, ""); }

            string[] file = File.ReadAllLines(fileName);//
            int maxId = -1;
            if (file.Length > 0)
            {
                string[] maxIdPerson = file[file.Length - 1].Split(';');//
                maxId = Convert.ToInt32(maxIdPerson[0]) >= 0 ? Convert.ToInt32(maxIdPerson[0]) + 1 : 0;
            }
            else
            {
                maxId = 0;
            }
            StreamWriter sw = File.AppendText(fileName);
            sw.Write(Convert.ToString(maxId) + ";" + _person);//
            //sw.WriteLine(_person);
            sw.Close();
            sw.Dispose();
            return "success";
        }

        static public string Read()
        {
            string[] file = File.ReadAllLines(fileName);
            string ListOfPerson = "";
            for (int i = 0; i < file.Length; i++)
            {
                ListOfPerson += file[i];
                if (file[i].Length > 0) 
                { ListOfPerson += "\n"; 
                }
            }
            return ListOfPerson;
        }

        static public string Delete(int id)
        {
            string[] file = File.ReadAllLines(fileName);
            bool flag = false;
            int posToDel = -1;
            for (int n = 0; n < file.Length; n++)
            {
                string[] temp = file[n].Split(';');
                if (temp[0] == Convert.ToString(id))
                {
                    flag = true;
                    posToDel = n;
                    break;
                }
            }
            if (!flag) { return "fail"; }

            File.Delete(fileName);
            FileInfo fi = new FileInfo(fileName);
            StreamWriter sw = fi.AppendText();
            for (int i = 0; i < file.Length; i++)
            {
                if (i == posToDel)
                {
                    continue;
                }
                sw.WriteLine(file[i]);
            }
            sw.Flush();
            sw.Close();
            return "success";
        }

        static public string Update(int id, string _newPerson)
        {
            string[] file = File.ReadAllLines(fileName);
            bool flag = false;

            for (int n = 0; n < file.Length; n++)
                if (file[n].Split(';')[0] == Convert.ToString(id))
                {
                    flag = true;
                    file[n] = Convert.ToString(id) + ";" + _newPerson;
                    break;
                }

            if (!flag) { return "fail"; }

            File.Delete(fileName);
            FileInfo fi = new FileInfo(fileName);
            StreamWriter sw = fi.AppendText();
            for (int i = 0; i < file.Length; i++)
            {
                sw.WriteLine(file[i]);
            }
            sw.Flush();
            sw.Close();
            return "success";
        }



        internal static string SafeFile(string name, byte[] file)
        {
            File.WriteAllBytes(name, file);
            return "success";
        }
    }//---- class DS_CSV_server
}
